/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.postcodes.stripes;

import com.sfs.postcodes.beans.ConfigurationBean;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.integration.spring.SpringBean;


/**
 *
 * @author David Harrison
 * 3 May 2008
 *
 */

public abstract class BaseActionBean implements ActionBean {

    /** The context. */
    private ActionBeanContext context;

    /** The messages. */
    private List<String> messages;

    /** The configuration. */
    private ConfigurationBean configuration;


    /**
     * Get the Stripes context.
     * @return ActionBeanContext the stripes context
     */
    public final ActionBeanContext getContext() {
        return this.context;
    }

    /**
     * Set the Stripes context.
     * @param contextRef the ActionBeanContext
     */
    public final void setContext(final ActionBeanContext contextRef) {
        this.context = contextRef;
    }

    /**
     * Gets the JSP file for the view.
     *
     * @param viewName the view name
     *
     * @return the view
     */
    protected final String getView(final String viewName) {
        return "/WEB-INF/views/" + viewName;
    }


    /**
     * Adds the message.
     *
     * @param message the message
     */
    protected final void addMessage(final String message) {
        if (this.messages == null) {
            this.messages = new ArrayList<String>();
        }
        this.messages.add(message);
    }

    /**
     * Gets the messages.
     *
     * @return the messages
     */
    protected final List<String> getMessages() {
        return this.messages;
    }

    /**
     * Gets the message list.
     *
     * @return the message list
     */
    public final String getMessageList() {
        StringBuffer sb = new StringBuffer();

        if (this.messages != null) {
            sb.append("<ul>");
            for (String message : this.messages) {
                sb.append("<li>");
                sb.append(message);
                sb.append("</li>");
            }
            sb.append("</ul>");
        }

        return sb.toString();
    }

    /**
     * Gets the display messages.
     *
     * @return the display messages
     */
    public final boolean getDisplayMessages() {
        boolean displayErrors = false;
        if (getContext().getValidationErrors() != null) {
            if (getContext().getValidationErrors().size() > 0) {
                displayErrors = true;
            }
        }
        if (getMessageList().compareTo("") != 0) {
            displayErrors = true;
        }
        return displayErrors;
    }

    /**
     * Setter to allow the ConfigurationBean to be injected.
     *
     * @param configurationRef the ConfigurationBean
     */
    @SpringBean("configuration")
    public final void injectConfiguration(final ConfigurationBean configurationRef) {
        this.configuration  = configurationRef;
    }

    /**
     * Gets the configuration.
     *
     * @return the configuration
     */
    public final ConfigurationBean getConfiguration() {
        if (this.configuration == null) {
            this.configuration = new ConfigurationBean();
        }
        return this.configuration;
    }

    /**
     * Gets the application url.
     *
     * @return the application url
     */
    public final String getApplicationUrl() {
        return getConfiguration().getApplicationUrl();
    }

    /**
     * Gets the theme.
     *
     * @return the theme
     */
    public final String getTheme() {
        return getConfiguration().getTheme();
    }

    /**
     * Gets the geocode api key.
     *
     * @return the geocode api key
     */
    public final String getGeocodeApiKey() {
        return getConfiguration().getGeocodeApiKey();
    }
}
